package com.example.demo.nios.selector2;

import com.sun.media.jfxmediaimpl.HostUtils;
import io.swagger.models.auth.In;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;

public class Client {
    public static void main(String[] args) {
        try {
            Integer port = 60000;
            String ip = "127.0.0.1";
            Charset charset = Charset.forName("UTF-8");
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            boolean flag = true;
            Selector selector = Selector.open();
            SocketChannel socketChannel = null;
            try {
                socketChannel = SocketChannel.open(new InetSocketAddress(ip,port));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(String.format("服务端 %s:%s 未启用",ip,port));
                return;
            }
            socketChannel.configureBlocking(false);
            socketChannel.write(charset.encode("fuck you"));
            socketChannel.register(selector, SelectionKey.OP_READ);
            while (flag) {
                selector.select();
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = iterator.next();
                    iterator.remove();
                    if(selectionKey.isConnectable()){
                        System.out.println("可链接");
                        SocketChannel channel = (SocketChannel) selectionKey.channel();
                        channel.close();

                    }
                    if(selectionKey.isValid()){
                        System.out.println("有效的");
                        SocketChannel channel = (SocketChannel) selectionKey.channel();
                        channel.close();
                    }

                    if (selectionKey.isReadable()) {
                        System.out.println("可读的");
                        SocketChannel channel = (SocketChannel) selectionKey.channel();
                        if (channel.read(byteBuffer) <= 0) {
                            selectionKey.cancel();
                            System.out.println(String.format("服务端 %s:%s断开", channel.socket().getInetAddress(), channel.socket().getPort()));
                            selector.close();
                            flag = false;
                        } else {
                            StringBuffer stringBuffer = new StringBuffer();
                            byteBuffer.flip();
                            stringBuffer.append(charset.decode(byteBuffer));
                            byteBuffer.clear();
                            System.out.println(String.format("接收到服务端 %s:%s,消息:%s", channel.socket().getLocalAddress(), channel.socket().getPort(), stringBuffer.toString()));
                            channel.write(charset.encode(stringBuffer.toString()));
                            if(stringBuffer.toString().equals("out")){
                                channel.close();
                                selectionKey.cancel();
                                selector.close();
                            }
                        }

                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
